<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/******************************************************
* Profile			: article 
* Author			: 乔伟
* Create Time		: 2011-10-05 
* Modify Time		: 2011-10-05 
* Modify Profile	:  
******************************************************/
class article extends Front_Controller {
	/******************************************************
	* Profile			: 列表
	* Author			: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05
	* Modify Profile	:  
	******************************************************/ 
	public function index($class_id = null)
	{
        $this->load->model('article_manager');
        $this->load->model('article_class_manager');
        $this->load->model('menu_manager');

        //  echo $class_id;exit;
        $where = $this->Request();
		if($class_id!=null)
		{
			$where["class.id"] = trim(str_replace(".html", "", $class_id));  
		}
		// print_r($where) ;  exit ; 
		$class_alias = array_remove($where, 'class.alias');
        $class_id = array_remove($where, 'class.id');

        //   echo $class_id ;exit;

		$article_class = null;
        $cache_key = "";
		if($class_id != null)
		{
			$article_class = $this->article_class_manager->get($class_id);
            $cache_key = $class_id;

		}
		if($class_alias != null)
		{
			$article_class = $this->article_class_manager->get(array('alias'=>$class_alias));
            $cache_key = $class_alias;
		}

		$tpl_index = "v_index";
		$part_template = "";
		if($article_class!=null)
		{
			$part_template =  $article_class->part_template;
			if(!empty($part_template) && 
				file_exists(APPPATH."views/".$this->template."/".CONTROL_FOLDER."/article/v_".$part_template."_index.php"))
			{
				$tpl_index = "v_".$part_template."_index";
			}
		}

		$article_index_html = $this->load->view("front/article/$tpl_index",array('article_class'=>$article_class),true);


        $page = isset($_GET['page'])?$this->input->get('page'):1;
        //  echo "article-index-$class_id-$page.html";exit;
        $this->cache->save("article-index-$cache_key-$page.html", $article_index_html, 3600*24*360);
        echo $article_index_html;
	}

	/******************************************************
	* Profile			: 显示
	* Author			: 乔伟
	* Create Time		: 2011-10-05 
	* Modify Time		: 2011-10-05 
	* Modify Profile	:  
	******************************************************/
	public function show($id = null)
	{
        $this->load->model('article_manager');
        $this->load->model('article_class_manager');
        $this->load->model('menu_manager');

        $id = isset($_GET['id'])?$this->input->get('id'):$id;
		$article = $this->article_manager->get($id);
		// $article->click();
		$tpl_show = "v_show";
		$part_template =  $article->class->part_template;
		if(!empty($part_template) && 
			file_exists(APPPATH."views/".$this->template."/".CONTROL_FOLDER."/article/v_".$part_template."_show.php"))
		{
			$tpl_show = "v_".$part_template."_show";
		}
		$article_html = $this->load->view("front/article/$tpl_show",array('article'=>$article),true);
        $this->cache->save('article-show-'.$id, $article_html, 3600*24*30);
        echo $article_html;
	}


    public function click($id = null){
        $this->load->model('article_manager');
        $id = isset($_GET['id'])?$this->input->get('id'):$id;
        $article = $this->article_manager->get($id);
        $click_count = $article->click();
        echo $click_count ;exit;
    }
}
